﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_RegexDAL : IDcms_RegexDAL
    {
        public Dcms_RegexModel AddModel(Dcms_RegexModel model)
        {
            string sQLString = "insert into Dcms_Regex(RegexName,RegexContent,RegexErrorMsg) values(@RegexName,@RegexContent,@RegexErrorMsg) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.SmallInt), new SqlParameter("@RegexName", SqlDbType.VarChar), new SqlParameter("@RegexContent", SqlDbType.VarChar), new SqlParameter("@RegexErrorMsg", SqlDbType.VarChar) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.RegexName;
            cmdParms[2].Value = model.RegexContent;
            cmdParms[3].Value = model.RegexErrorMsg;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.RegexID = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_RegexModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_RegexModel> list = new List<Dcms_RegexModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_RegexModel item = new Dcms_RegexModel();
                if (ds.Tables[0].Rows[i]["RegexID"].ToString() != "") item.RegexID = int.Parse(ds.Tables[0].Rows[i]["RegexID"].ToString());
                item.RegexName = ds.Tables[0].Rows[i]["RegexName"].ToString();
                item.RegexContent = ds.Tables[0].Rows[i]["RegexContent"].ToString();
                item.RegexErrorMsg = ds.Tables[0].Rows[i]["RegexErrorMsg"].ToString();
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Regex where RegexID=@RegexID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RegexID", SqlDbType.SmallInt) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public Dcms_RegexModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Regex where RegexID=@RegexID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RegexID", SqlDbType.SmallInt) };
            cmdParms[0].Value = ID;
            Dcms_RegexModel model = new Dcms_RegexModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_RegexModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public IList<Dcms_RegexModel> GetModelList()
        {
            string sql = "select * from Dcms_Regex";
            return this.DataSetToList(SqlHelper.CreateDataSet(sql));
        }

        public Dcms_RegexModel Update(Dcms_RegexModel model)
        {
            string sQLString = "update Dcms_Regex set RegexName=@RegexName,RegexContent=@RegexContent,RegexErrorMsg=@RegexErrorMsg where RegexID=@RegexID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RegexName", SqlDbType.VarChar), new SqlParameter("@RegexContent", SqlDbType.VarChar), new SqlParameter("@RegexErrorMsg", SqlDbType.VarChar), new SqlParameter("@RegexID", SqlDbType.SmallInt) };
            cmdParms[0].Value = model.RegexName;
            cmdParms[1].Value = model.RegexContent;
            cmdParms[2].Value = model.RegexErrorMsg;
            cmdParms[3].Value = model.RegexID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

